package com.pega.eprescription;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.pega.eprescription.activity.DailyRemindActivity;
import com.pega.eprescription.activity.EditDrug;
import com.pega.eprescription.activity.EditPrescription;
import com.pega.eprescription.activity.SettingActivity;
import com.pega.eprescription.activity.dialog.TimePickerFragment;
import com.pega.eprescription.alarm.AlarmReceiver;
import com.pega.eprescription.database.manager.DatabaseManager;
import com.pega.eprescription.database.model.Pill;
import com.pega.eprescription.database.model.Prescription;
import com.pega.eprescription.utils.Utils;

public class MainActivity extends FragmentActivity implements
		NumberPicker.OnValueChangeListener {

	private TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Intent intent = new Intent(getApplicationContext(),EditPrescription.class);
		intent.putExtra("PreID", -1);
		startActivity(intent);
		
	Prescription pres = new Prescription("Headache", true, Utils.sharedInstance().getCurrentDate(), "9:00", "11:00", "19:00");
	int id  = DatabaseManager.getInstance(this).addPrescriptionRecord(pres);
	Pill pill = new Pill(id, 30, 30	, 1, 1, 1, "abc", "Bipyridine");
	DatabaseManager.getInstance(this).addPillRecord(pill);
	
	pill = new Pill(id, 50, 50	, 2, 1, 2, "abc1", "Pyridostigmine");
	DatabaseManager.getInstance(this).addPillRecord(pill);
	
	pill = new Pill(id, 40, 40	, 1, 2, 1, "abc3", "Abacavir");
	DatabaseManager.getInstance(this).addPillRecord(pill);
	
	Prescription pres1 = new Prescription("Lithicosis", true, Utils.sharedInstance().getCurrentDate(), "9:00", "11:00", "19:00");
	int id1  = DatabaseManager.getInstance(this).addPrescriptionRecord(pres1);
	pill = new Pill(id1, 40, 40	, 1, 1, 2, "abc", "Idapamide"	);
	DatabaseManager.getInstance(this).addPillRecord(pill);

	
	pill = new Pill(id1, 30, 30	, 1, 1, 1, "abc3", "Perindopril"	);
	DatabaseManager.getInstance(this).addPillRecord(pill);
	
	
	Button btnAddNew = (Button)findViewById(R.id.addPrescription);
	btnAddNew.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		}
	});
//		Button b = (Button) findViewById(R.id.button11);
//		b.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				new AlarmReceiver().setAlarm(MainActivity.this);
//				
//				//startActivity(new Intent(getApplicationContext(), DailyRemindActivity.class));
//				
//				/*new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
//					
//					@Override
//					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//						tv.setText(hourOfDay + ":" + minute);
//					}
//				}, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), true).show();*/
//			}
//		});

		ArrayList<Prescription> allPrescription = DatabaseManager.getInstance(getApplicationContext()).getAllPrescriptionRecord();
		IllnessListAdapter adapter= new IllnessListAdapter(getApplicationContext(), allPrescription);
		ListView listView = (ListView) findViewById(R.id.listview);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),EditPrescription.class);
				intent.putExtra("PreID", arg2);
				startActivity(intent);				
			}
			
		});
		// Intent intent = new Intent(this, EditDrug.class);
		// startActivity(intent);
	}

	public void showTimePickerDialog() {
		DialogFragment newFragment = new TimePickerFragment();
		newFragment.show(this.getSupportFragmentManager(), "timePicker");
	}

	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

		//Log.i("value is", "" + newVal);

	}

	public void show() {

		final Dialog d = new Dialog(MainActivity.this);
		d.setTitle("NumberPicker");
		d.setContentView(R.layout.dialog_numberpicker);
		Button b1 = (Button) d.findViewById(R.id.button1);
		Button b2 = (Button) d.findViewById(R.id.button2);
		final NumberPicker np = (NumberPicker) d
				.findViewById(R.id.numberPicker1);
		np.setMaxValue(100);
		np.setMinValue(0);
		np.setWrapSelectorWheel(false);
		np.setOnValueChangedListener(this);
		b1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tv.setText(String.valueOf(np.getValue()));
				d.dismiss();
			}
		});
		b2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				d.dismiss();
			}
		});
		d.show();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		
		case R.id.main_calendar:
			break;
			
		case R.id.setting: 
			  startActivity(new Intent(this, SettingActivity.class));
			  break;

		default:
			break;
		}
		return false;
	}

}
